<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-3-18
 * Time: 下午4:33
 */

namespace front\controllers;
use EasyWeChat\Foundation\Application;
use wechat\services\AccountService;
use Yii;
use common\WebController;
use users\services\UserService;
use yii\helpers\Json;


/**
 * BannerController implements the CRUD actions for BannerModel model.
 */
header("Content-type:text/html;charset=utf-8");
class WebBaseController extends WebController{
    const ORIGINAL_ID = 'gh_0e3181f3b31b';
    private $user_id;
    public $shop_id=1001;
    public $enableCsrfValidation = false;
    public $openId;
    public $token_time;
    public $token;
    public $appId;
    public $appSecret;
    public $photo;
    function init(){
        parent::init();
//        if (Yii::$app->user->isGuest){
//            return $this->redirect(['/site/login']);
//        }
        //$this->user_id = UserService::getUserId();
        $this->openId = \Yii::$app->session->get('openId');
        $this->token_time = \Yii::$app->session->get('token_time');
        $this->token = \Yii::$app->session->get('token');
        $this->appId = \Yii::$app->session->get('appId');
        $this->appSecret = \Yii::$app->session->get('appSecret');
        $this->photo = \Yii::$app->session->get('photo');
    }

    public function getUserId()
    {
        if(yii::$app->session->has('user_id')){
            $this->user_id = yii::$app->session->get('user_id');
            return $this->user_id;
        }
        else if(!yii::$app->user->isGuest){
            $this->user_id = UserService::getUserId();
            return $this->user_id;
        }
        else {
            return 0;
        }
    }

    //格式化打印
    static function P($data){
        echo "<!--";
        echo "<pre>";
        print_r($data);
        echo "</pre";
        echo "-->";
    }
    //jsonp回调 status状态码 $message 返回状态信息 $data 返回状态数据

    public static function getAjaxData($status, $message = '', $data = '',$func='',$count='',$type='',$pay=''){
        $data['status'] = $status;
        $data['message'] = $message;
        $data['count']= $count;
        $data['type']= $type;
        $data['pay']= $pay;
        echo trim($func).'('.Json::encode($data).');';
        exit();

    }

    //json回调 status状态码 $message 返回状态信息 $data 返回状态数据

    public static function getAjaxDataInfo($status, $message = '', $data = '',$callback=''){
        $res['status'] = $status;
        $res['code'] = $status; //APP使用
        $res['message'] = $message;
        $res['data'] = $data;
        $res['callback'] = $callback;
        echo Json::encode($res);
        exit();
    }


    //格式化产品图片
    static function getImgPath($data){
        if(!$data){
            return false;
        }
        return  'http://static.shopbox.10000dt.com/goods/'.$data;
    }
    public function formName()
    {
        return '';
    }

    /**
     * 格式化商品价格
     *
     * @access  public
     * @param   float   $price  商品价格
     * @return  string
     */
    static function price_format($price) {
        if ($price === '') {
            $price = 0;
        }
        $price = 0 + $price;//添加这一行，转换成数值
        return sprintf("%.2f", $price);
    }


    /*
    * @name 字符串截取函数
    * $str $string 待切割的字符串
    * $length 切割长度
    */
    public static function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
    {
        if(function_exists("mb_substr")){
            $num = mb_strlen($str,$charset);
            if($num > $length && $suffix){
                return mb_substr($str, $start, $length, $charset)."...";
            }
            else
                return mb_substr($str, $start, $length, $charset);
        }
        elseif(function_exists('iconv_substr')) {
            $num = mb_strlen($str,$charset);
            if($num > $length && $suffix){
                return iconv_substr($str,$start,$length,$charset)."...";
            }
            else
                return iconv_substr($str,$start,$length,$charset);
        }
        $re['utf-8']   = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
        $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
        $re['gbk']    = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
        $re['big5']   = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
        if($suffix) return $slice."…";
        return $slice;
    }

}

